package com.dl.mall.controller.user;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.config.satoken.StpUserUtil;
import com.dl.mall.entity.vo.CartVo;
import com.dl.mall.service.user.CartService;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DL
 * @create 2022-03-24 20:21
 */
@Slf4j
@RestController()
@RequestMapping("/cart")
@Api(tags="购物车controller")
public class CartController {

    @Resource
    private CartService cartService;

    @ApiOperation(value="根据 用户id + 产品规格id 减少购物车商品的数量")
    @MethodInfo
    @PostMapping("/decrease")
    @SentinelResource(value = "limiting")
    public Result decreaseCart(@RequestBody JSONObject jsonObject){
        String specsId = jsonObject.getString("specsId");
        boolean decreaseCart = cartService.decreaseCart(StpUserUtil.getLoginIdAsString(), specsId);

        if (decreaseCart){
            return Result.ok("减少数量成功");
        }else {
            return Result.error("减少数量失败");
        }
    }

    @ApiOperation(value="根据 用户id + 产品规格id 增加购物车商品的数量")
    @MethodInfo
    @PostMapping("/increase")
    @SentinelResource(value = "limiting")
    public Result increaseCart(@RequestBody JSONObject jsonObject){
        String specsId = jsonObject.getString("specsId");
        boolean increaseCart = cartService.increaseCart(StpUserUtil.getLoginIdAsString(), specsId);

        if (increaseCart){
            return Result.ok("增加数量成功");
        }else {
            return Result.error("增加数量失败");
        }
    }

    @ApiOperation(value="根据 用户id + 产品规格id 增加购物车商品")
    @MethodInfo
    @PostMapping("/add")
    @SentinelResource(value = "limiting")
    public Result addCart(@RequestBody JSONObject jsonObject){
        String specsId = jsonObject.getString("specsId");
        // 添加前进行逻辑判断
        boolean addCart = cartService.addCart(StpUserUtil.getLoginIdAsString(), specsId, true);

        if (addCart){
            return Result.ok("添加购物车成功");
        }else {
            return Result.error("添加购物车失败");
        }
    }

    @ApiOperation(value="根据 用户id + 产品规格id （批量）删除购物车商品")
    @MethodInfo
    @PostMapping("/remove")
    @SentinelResource(value = "limiting")
    public Result removeCart(@RequestBody JSONObject jsonObject){
        System.out.println("jsonObject = " + jsonObject);
        JSONArray specsIdList = jsonObject.getJSONArray("specsIdList");
        System.out.println("specsIdList = " + specsIdList);
        boolean removeCart = cartService.removeCart(StpUserUtil.getLoginIdAsString(), specsIdList);

        if (removeCart){
            return Result.ok("删除商品成功");
        }else {
            return Result.error("删除商品失败");
        }
    }

    @ApiOperation(value="根据 用户id 查询全部购物车")
    @MethodInfo
    @GetMapping("/all")
    @SentinelResource(value = "limiting")
    public Result allCart(){
        List<CartVo> cartVoList = cartService.getAllCart(StpUserUtil.getLoginIdAsString());

        if (cartVoList != null){
            return Result.ok("查询购物车成功").data("cartVoList",cartVoList);
        }else {
            return Result.error("查询购物车失败");
        }
    }

    @ApiOperation(value="根据 用户id 查询全部购物车")
    @MethodInfo
    @GetMapping("/getCartByUserId")
    @SentinelResource(value = "limiting")
    public Result getCartByUserId(String userId){
        List<CartVo> cartVoList = cartService.getAllCart(userId);

        if (cartVoList != null){
            return Result.ok("查询购物车成功").data("cartVoList",cartVoList);
        }else {
            return Result.error("查询购物车失败");
        }
    }
}
